package exam.rao.perparedata;

import com.rao.RaoStudentMangerApp;
import com.rao.entity.*;
import com.rao.mapper.*;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Random;

@SpringBootTest(classes = RaoStudentMangerApp.class)
@RunWith(SpringRunner.class)
public class PrepareData {

    @Resource
    SqlSessionFactory sqlSessionFactory;

    @Test
    public void insertBatchStudent() throws ParseException {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Random random = new Random();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 1; i <= 10; i++) {
            String date = String.format("2000-%d-%d", i, random.nextInt(28));
            Date parse = dateFormat.parse(date);
            Instant instant = parse.toInstant();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            mapper.insert(new Student(null, "学生003-" + i, random.nextInt(2), "134xxx" + i,
                    localDateTime, "杭州", null, null, null, null, null, null, 1));
        }
        for (int i = 1; i <= 10; i++) {
            String date = String.format("1999-%d-%d", i, random.nextInt(28));
            Date parse = dateFormat.parse(date);
            Instant instant = parse.toInstant();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            mapper.insert(new Student(null, "学生002-" + i, random.nextInt(2), "157xxx" + i,
                    localDateTime, "杭州", null, null, null, null, null, null, 2));
        }
        for (int i = 1; i <= 10; i++) {
            String date = String.format("2001-%d-%d", i, random.nextInt(28));
            Date parse = dateFormat.parse(date);
            Instant instant = parse.toInstant();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            mapper.insert(new Student(null, "学生001-" + i, random.nextInt(2), "147xxx" + i,
                    localDateTime, "杭州", null, null, null, null, null, null, 3));
        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void insertBatchTeacher() throws ParseException {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
        Random random = new Random();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 1; i <= 3; i++) {
            String date = String.format("2000-%d-%d", i, random.nextInt(28));
            Date parse = dateFormat.parse(date);
            Instant instant = parse.toInstant();
            LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
            mapper.insert(new Teacher());
        }

        sqlSession.commit();
        sqlSession.close();
    }

    /**
     * 准备clazz的数据
     */
    @Test
    public void insertBatchClazz() {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        ClazzMapper mapper = sqlSession.getMapper(ClazzMapper.class);
        for (int i = 1; i <= 3; i++) {
            mapper.insert(new Clazz(null, 10, "高[三]:" + (i) + "班"));
        }
        for (int i = 1; i <= 3; i++) {
            mapper.insert(new Clazz(null, 11, "高[二]:" + (i) + "班"));
        }
        for (int i = 1; i <= 3; i++) {
            mapper.insert(new Clazz(null, 12, "高[一]:" + (i) + "班"));
        }
        sqlSession.commit();
        sqlSession.close();
    }

    @Resource
    UserMapper userMapper;

    @Test
    public void insertUser() {
        User entity = new User();

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        entity.setUsername("rao3030");
        entity.setPassword(passwordEncoder.encode("rao3030"));
        userMapper.insert(entity);
    }

    @Resource
    CourseMapper courseMapper;

    @Test
    public void insertCourse() {
        courseMapper.insert(new Course(null, "数学", 150, true));
        courseMapper.insert(new Course(null, "语文", 150, true));
        courseMapper.insert(new Course(null, "英语", 150, true));
        courseMapper.insert(new Course(null, "音乐", 100, false));
    }

    @Resource
    ScoreMapper scoreMapper;

    @Test
    public void insertScore() {
        LocalDateTime now = LocalDateTime.now();
        scoreMapper.insert(new Score(null, 1, 1, 120.0, now));
        scoreMapper.insert(new Score(null, 1, 2, 120.0, now));
        scoreMapper.insert(new Score(null, 1, 3, 120.0, now));
        scoreMapper.insert(new Score(null, 1, 4, 88.0, now));

        scoreMapper.insert(new Score(null, 2, 1, 120.0, now));
        scoreMapper.insert(new Score(null, 2, 2, 120.0, now));
        scoreMapper.insert(new Score(null, 2, 3, 120.0, now));
        scoreMapper.insert(new Score(null, 2, 4, 88.0, now));

        scoreMapper.insert(new Score(null, 3, 1, 120.0, now));
        scoreMapper.insert(new Score(null, 3, 2, 120.0, now));
        scoreMapper.insert(new Score(null, 3, 3, 120.0, now));
        scoreMapper.insert(new Score(null, 3, 4, 88.0, now));

        scoreMapper.insert(new Score(null, 4, 1, 120.0, now));
        scoreMapper.insert(new Score(null, 4, 2, 120.0, now));
        scoreMapper.insert(new Score(null, 4, 3, 120.0, now));
        scoreMapper.insert(new Score(null, 4, 4, 88.0, now));
    }
}
